package com.hctg.forced2sleep.activity;

import android.content.Intent;
import android.os.Bundle;

import com.actor.myandroidframework.bean.OnActivityCallback;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.bumptech.glide.Glide;
import com.hctg.forced2sleep.adapter.AppMonitorSetAdapter;
import com.hctg.forced2sleep.bean.MineItem;
import com.hctg.forced2sleep.databinding.ActivityAppMonitorSetBinding;
import com.hctg.forced2sleep.dialog.HourMinuteWeekSelectDialog;
import com.hctg.forced2sleep.dialog.LimitEditDialog;
import com.hctg.forced2sleep.dialog.Tips1BtnDialog;

import java.util.Arrays;
import java.util.List;

/**
 * description: 首页 -> 监督(点击app图标) -> 应用监督修改
 * company    :
 * @author    : ldf
 * date       : 2025/1/2 on 14:37
 */
public class AppMonitorSetActivity extends BaseActivity<ActivityAppMonitorSetBinding> {

    public static final String RESULT_KEY = "item";
    private final List<String> options = Arrays.asList("停用至凌晨", "停用1分钟", "停用5分钟", "停用30分钟", "停用60分钟", "停用120分钟", "停用300分钟");
    private final List<Long> optionsMs = Arrays.asList(0L, 60000L, 300000L, 1800000L, 3600000L, 7200000L, 18000000L);

    private final AppMonitorSetAdapter mAdapterUsageLimit = new AppMonitorSetAdapter();

    public static void start(BaseActivity<?> activity, MineItem item, OnActivityCallback callback) {
        if (item == null) return;
        activity.startActivityForResult(new Intent(activity, AppMonitorSetActivity.class)
                .putExtra(RESULT_KEY, GsonUtils.toJson(item))
                , callback);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String json = getIntent().getStringExtra(RESULT_KEY);
        MineItem item = GsonUtils.fromJson(json, MineItem.class);

        viewBinding.titleBar.ivBack.setOnClickListener(v -> finish());
        viewBinding.titleBar.setTitle("应用监督修改");

        //限制修改
        viewBinding.tvLimitTime.setOnClickListener(v -> {
            new LimitEditDialog(mActivity).show();
        });

        //使用时长限制
        viewBinding.ivUsageLimit.setOnClickListener(v -> {
            new Tips1BtnDialog(this,
                    "使用时长限制",
                    "设置时长限制后，在指定的星期几只能使用所对应的时长，超过时间就会无法打开App。" +
                            "例如：可以设置周一至周五某App可以用6个小时，周六至周日可以用12小时。",
                    null
            ).show();
        });

        // FIXME: 2025/1/8
        //添加可用时长
        viewBinding.tvAddUsageLimit.setOnClickListener(v -> {
//            aVar.content = "当前时间段处于限制修改时间段，不可修改/删除，仅能添加，是否确定要添加[可用时长]呢？";
//            aVar.l = null;
//            aVar.title = "温馨提示";
            new HourMinuteWeekSelectDialog(this, "设置每日最大使用时长", 8, 0, null, (hour, minute, weekBean) -> {
                mAdapterUsageLimit.addData("");
            }).show();
        });
        //时间消耗完后
        viewBinding.llStopTo.setOnClickListener(v -> {
            new HourMinuteWeekSelectDialog(this, "设置每日最大使用时长", 8, 0, null, (hour, minute, weekBean) -> {
                mAdapterUsageLimit.addData("");
            }).show();
        });


        //今日已使用时长
        viewBinding.tvTodayUse.setText("35分钟");

        //icon
        if (item != null) {
            Glide.with(this)
                    .load(AppUtils.getAppIcon(item.subName))
                    .circleCrop()
                    .into(viewBinding.ivIcon);
        }

        //使用时长限制
        viewBinding.recyclerViewUsageLimit.setAdapter(mAdapterUsageLimit);
    }
}